﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using AppCommon;
using Controller;

public partial class Tour_detail : System.Web.UI.Page
{
    public static string urlWebsite = ConfigurationManager.ConnectionStrings["URLWebsite"].ToString();
    public static string linkTour;
    public static string _meta_description;
    public static string _meta_title;
    public static string _meta_image;
    public static string _meta_url;    

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            var uniqueIdTour = Request.QueryString["uniqueid"];
            if (uniqueIdTour != null)
            {
                BindData(uniqueIdTour);
                BindTintucnoibat();
            }
            BindQuangCao01(Constants.QC2);
            BindQuangCao02(Constants.QC3);
            BindQuangCao03(Constants.QC4);
        }
    }

    public void BindData(string uniqueId)
    {
        try
        {
            var controller = new VYC01Controller();
            var model = controller.GetByUniqueId(uniqueId, 1);

            if (model.Count > 0)
            {
                litTitlePage.Text = model[0].TieuDe;
                _meta_title = model[0].TieuDe;
                litTieude.Text = model[0].TieuDe;
                _meta_description = model[0].TomTat;
                litNgaycapnhat.Text = model[0].NgayCapNhat.ToString("dd/MM/yyyy");
                litTacgia.Text = GetHoten(model[0].IdUser); // urlWebsite
                litTongsongay.Text = Convert.ToInt32((model[0].NgayDen - model[0].NgayDi).TotalDays + 1).ToString() + " ngày";
                litGia.Text = String.Format("{0:###,###,##0}", model[0].Gia);
                litNgaydi.Text = model[0].NgayDi.ToString("dd/MM/yyyy");
                litNgayden.Text = model[0].NgayDen.ToString("dd/MM/yyyy");
                litNoidung.Text = Server.HtmlDecode(model[0].NoiDung);
                litAnhdaidien.ImageUrl = Constants.UPLOAD_TOUR + model[0].AnhDaiDien;
                _meta_image = urlWebsite + "Upload/Tour/" + model[0].AnhDaiDien;
                _meta_url = urlWebsite + "tour/" + model[0].UniqueId;
                litNoidi.Text = GetNameDiadiem(model[0].IdNoiDi);
                litNoiden.Text = GetNameDiadiem(model[0].IdNoiDen);
                linkDattour.NavigateUrl = "~/dattour/" + model[0].Id + "/dangky";

                BindGioithieudiemden(model[0].IdNoiDen);
                BindTourCDD(model[0].IdNoiDen);
                BindTourCMG(model[0].Gia);
                BindTourCDKH(model[0].NgayDi);
                BindLichKhoiHanh(model[0].Id);
                BindNhungdieucanbiet(model[0].Id);
                BindVideoLienQuan(model[0].IdSubLoaiTour);
                BindAlbumAnhLienQuan(model[0].IdSubLoaiTour);

                repeaterTags.DataSource = SpilitTags(model[0].Tag);
                repeaterTags.DataBind();
                // 
                linkTour = urlWebsite + "tour/" + model[0].UniqueId; //urlWebsite + "tour/" + model[0].UniqueId;
            }
            else
            {
                Response.Redirect("~/404.aspx");
            }
        }
        catch (Exception)
        {
            
            throw;
        }
    }

    /// <summary>
    /// Lich khoi hanh
    /// </summary>
    /// <param name="idTour"></param>
    public void BindLichKhoiHanh(string idTour)
    {
        var controller = new VYC0101Controller();
        var model = controller.GetByIdTour(idTour, 1);
        if (model.Count > 0)
        {
            // Bind danh sach khoi hanh cua tour
            repeaterLkh.DataSource = model;
            repeaterLkh.DataBind();
        }
    }

    public void BindNhungdieucanbiet(string idTour)
    {
        var controller = new VYC25Controller();
        var model = controller.GetByIdTour(idTour);

        if (model.Count > 0)
        {
            litInfo.Text = Server.HtmlDecode(model[0].GioiThieu);
            litVisa.Text = Server.HtmlDecode(model[0].Visa);
            litLuuy.Text = Server.HtmlDecode(model[0].LuuY);
            litDieukhoan.Text = Server.HtmlDecode(model[0].DieuKhoan);
            litMeovat.Text = Server.HtmlDecode(model[0].MeoVat);
        }
        else
        {
            litInfo.Text = "<b>Thông tin chưa được cập nhật</b>";
            litVisa.Text = "<b>Thông tin chưa được cập nhật</b>";
            litLuuy.Text = "<b>Thông tin chưa được cập nhật</b>";
            litDieukhoan.Text = "<b>Thông tin chưa được cập nhật</b>";
            litMeovat.Text = "<b>Thông tin chưa được cập nhật</b>";
        }
    }

    public void BindChiasetuKH(string idTour)
    {
        
    }

    /// <summary>
    /// Gioi thieu ve diem den
    /// </summary>
    /// <param name="idDiemden"></param>
    public void BindGioithieudiemden(int idDiemden)
    {
        var controller = new VYC24Controller();
        var model = controller.GetById(idDiemden);
        if (model.Count > 0)
        {
            litGioithieudiemden.Text = Server.HtmlDecode(model[0].NoiDung);
            litLatlong.Text = model[0].TungDo + "," + model[0].ViDo;
        }
        else
        {
            litGioithieudiemden.Text = "Chưa cập nhật thông tin điểm đến";
        }
    }

    /// <summary>
    /// Danh sach cac tour co cung diem den
    /// </summary>
    /// <param name="idDiemden"></param>
    public void BindTourCDD(int idDiemden)
    {
        var controller = new VYC01Controller();
        var model = controller.GetByDiemden(idDiemden, 1);
        if (model.Count > 0)
        {
            repeaterTourCDD.DataSource = model;
            repeaterTourCDD.DataBind();
        }
        else
        {
            // Do something
        }
    }

    /// <summary>
    /// Danh sach cac tour co cung muc gia
    /// </summary>
    /// <param name="gia"></param>
    public void BindTourCMG(decimal gia)
    {
        var controller = new VYC01Controller();
        var model = controller.GetByGia(gia, 1);
        if (model.Count > 0)
        {
            repeaterTourCMG.DataSource = model;
            repeaterTourCMG.DataBind();
        }
        else
        {
            // Do something
        }
    }
    /// <summary>
    /// Tour cung dip khoi hanh
    /// </summary>
    /// <param name="ngaydi"></param>
    public void BindTourCDKH(DateTime ngaydi)
    {
        var controller = new VYC01Controller();
        var model = controller.GetByNgayDi(ngaydi, 1);
        if (model.Count > 0)
        {
            repeaterTourCNKH.DataSource = model;
            repeaterTourCNKH.DataBind();
        }
        else
        {
            // Do something
        }
    }
    // end bind data cho Tour lien quan ----/

    public void BindTintucnoibat()
    {
        var controller = new VYC06Controller();
        var model = controller.GetByView();

        if (model.Count > 0)
        {
            repeaterTintuc.DataSource = model;
            repeaterTintuc.DataBind();            
        }
    }

    public string GetNameDiadiem(int id)
    {
        var controller = new VYC24Controller();
        var model = controller.GetById(id);
        return model.Count>0 ? model[0].Ten : "NA";
    }

    public string CheckStatusDatetime(DateTime dateTime)
    {
        if (dateTime > DateTime.Now)
        {
            return "<i class=\"label label-success\">Còn hạn</i>";
        }
        else
        {
            return "<i class=\"label label-danger\">Liên hệ</i>";
        }
    }
    public List<String> SpilitTags(string tag)
    {
        var tags = tag.Split(',');
        return tags.Select(s => s.Replace(' ', '-')).ToList();
    }

    public string GetHoten(string username)
    {
        var controller = new VYC17Controller();
        var model = controller.GetByUsername(username);
        if (model.Count > 0)        
            return model[0].HoTen;
        return "VYCUser";
    }

    #region Quangcao

    public void BindQuangCao01(string uniqueid)
    {
        var controller = new VYC05Controller();
        var model = controller.GetByUniqueId(uniqueid);

        // Get quang cao 1
        if (model.Count > 0)
        {
            var controller1 = new VYC06Controller();
            var model1 = controller1.GetBySubLoaiCm(model[0].Id, 1);

            // Set link and image
            if (model1.Count > 0)
            {
                linkQuangcao01.NavigateUrl = model1[0].LienKet;
                imgQuangcao01.ImageUrl = Constants.UPLOAD_TINTUC + model1[0].AnhDaiDien;
                imgQuangcao01.AlternateText = model1[0].TieuDe;
                imgQuangcao01.ToolTip = model1[0].TieuDe;
            }
        }
    }
    public void BindQuangCao02(string uniqueid)
    {
        var controller = new VYC05Controller();
        var model = controller.GetByUniqueId(uniqueid);

        // Get quang cao 1
        if (model.Count > 0)
        {
            var controller1 = new VYC06Controller();
            var model1 = controller1.GetBySubLoaiCm(model[0].Id, 1);

            // Set link and image
            if (model1.Count > 0)
            {
                linkQuangcao02.NavigateUrl = model1[0].LienKet;
                imgQuangcao02.ImageUrl = Constants.UPLOAD_TINTUC + model1[0].AnhDaiDien;
                imgQuangcao02.AlternateText = model1[0].TieuDe;
                imgQuangcao02.ToolTip = model1[0].TieuDe;
            }

        }
    }
    public void BindQuangCao03(string uniqueid)
    {
        var controller = new VYC05Controller();
        var model = controller.GetByUniqueId(uniqueid);

        // Get quang cao 1
        if (model.Count > 0)
        {
            var controller1 = new VYC06Controller();
            var model1 = controller1.GetBySubLoaiCm(model[0].Id, 1);

            // Set link and image
            if (model1.Count > 0)
            {
                linkQuangcao03.NavigateUrl = model1[0].LienKet;
                imgQuangcao03.ImageUrl = Constants.UPLOAD_TINTUC + model1[0].AnhDaiDien;
                imgQuangcao03.AlternateText = model1[0].TieuDe;
                imgQuangcao03.ToolTip = model1[0].TieuDe;
            }
        }
    }

    #endregion

    public void BindVideoLienQuan(int idsubloaitour)
    {
        var controller = new VYC23Controller();
        var model = controller.GetLimitVideoForTourDetail(1, idsubloaitour);

        if (model.Count > 0)
        {
            repeaterVideos.DataSource = model;
            repeaterVideos.DataBind();
        }
        else
        {
            //model = controller.GetLimitVideoForModule(1);
            //repeaterVideos.DataSource = model;
            //repeaterVideos.DataBind();
            litNovideo.Text = "<p>Không tìm thấy video nào liên quan.</p>";
        }
    }

    public void BindAlbumAnhLienQuan(int idsubloaitour)
    {
        var controller = new VYC21Controller();
        var model = controller.GetLimitAlbumForTourDetail(1, idsubloaitour);

        if (model.Count > 0)
        {
            repeaterAlbum.DataSource = model;
            repeaterAlbum.DataBind();
        }
        else
        {
            litNoAlbum.Text = "<p class=\"text-danger\">Không tìm thấy album ảnh nào liên quan.</p>";
        }
    }
}
